package com.grad.why.studentsystem.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalControllerExceptionHandler {
    public static final String DEFAULT_ERROR_VIEW = "error"; 
   
    @ExceptionHandler(value = Exception.class)
    public Object defaultErrorHandler(HttpServletRequest request, Exception exception) throws Exception {
        exception.printStackTrace();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("url", request.getRequestURL());
        modelAndView.addObject("exception", exception);
        modelAndView.setViewName(DEFAULT_ERROR_VIEW);
        System.out.println("经过GlobalControllerExceptionHandler");
        return modelAndView;
    }
    /**
     * 
     * @Description Ajax请求的判断 
     * @param request
     * @return
     */
    public static  boolean isAjax(HttpServletRequest request){
    	boolean isAjax = (request.getHeader("X-Requested-With") != null
    			&& "XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
    	return isAjax;
    }
}
